myApp.onPageInit('relay-item-list', function (page) {
    'use strict';

    console.log("onPageInit " + page.name);

    var list = $$(page.container).find('.list-block'),
        empty = $$(page.container).find('.empty-view'),
        refreshContainer = $$(page.container).find('.pull-to-refresh-content');
    list.data('request', null);
    list.data('lastpage', false);
    list.data('nextpage', 0);

    function loadPage() {
        var refresing = list.data('nextpage') == 0,
            req = true;
        $$.getJSONP(SERVER_PATH + 'getDocRelayList.do', {num: PAGE_SIZE, start: (list.data('nextpage') * PAGE_SIZE)}, function (json) {
            if (json.code == 1) {
                list.data('nextpage', list.data('nextpage') + 1);

                if (refresing) {
                    list.find('ul').children().remove();
                }

                $$.each(json.results, function (index, value) {
                    if (value.isLastPage) {
                        list.data('lastpage', true);
                    }

                    preprocessData('docRelayList', value);
                    var html = Template7.templates.relayItemTemplate(value);
                    list.find('ul').append(html);
                });
                empty[list.find('ul').children().length > 0 ? 'hide' : 'show']();
            }

            list.data('request', null);
            if (refresing) {
                myApp.pullToRefreshDone();
            }
        });
        list.data('request', req);
    }

    list.on('click', 'a.item-link', function () {
        $$(this).find('.badge').hide();
    });

    $$(page.container).find('.infinite-scroll').on('infinite', function () {
        if (list.data('lastpage') || list.data('request')) {
            return;
        }

        loadPage();
    });
    refreshContainer.on('refresh', function (e) {
        var request = list.data('request');
        if (request) {
            request.abort();
        }
        list.data('request', null);
        list.data('lastpage', false);
        list.data('nextpage', 0);
        loadPage();
    });
});

myApp.onPageAfterAnimation("relay-item-list", function (page) {
    console.log('pageAfterAnimation ' + page.name);
    myApp.pullToRefreshTrigger($$(page.container).find('.pull-to-refresh-content'));
});

myApp.onPageAfterBack('relay-item-detail', function (page) {
	$$('.page[data-page="relay-item-list"] .pull-to-refresh-content').trigger('refresh');
});

myApp.onPageBeforeInit('relay-item-detail', function (page) {
    if (myApp.padApp) {
        $$(page.navbarInnerContainer).find('.left').css('visibility', 'hidden');
    }
});
myApp.onPageAfterAnimation('relay-item-detail', function (page) {
    if (!localStorage.getItem('popover-menu-showed')) {
        localStorage.setItem('popover-menu-showed', true);
        myApp.popover('.popover-menu', $$(page.navbarInnerContainer).find('.right'));
    }
});
myApp.onPageInit('relay-item-detail', function (page) {
    console.log("onPageInit " + page.name);
    var params = page.query,
        data;

    $$(page.container).find('.tab1').on('show', function () {
        $$(page.navbarInnerContainer).find('.center').text('待阅事宜');
    });
    
    if(page.query.weboffice == "false"){      	
    	$$(page.navbarInnerContainer).find('.i-content').css("display","none");
    }
    
    $$(page.container).find('.tab2').on('show', function () {
        $$(page.navbarInnerContainer).find('.center').text('正文');
        var tab = $$(this),
            iframe = tab.find('iframe'),
            empty = tab.find('.empty-view');

        if (data) {
            $$.getJSONP(SERVER_PATH + 'hasFileReleasePub.do', {
                formDefId: data.formValues.hiddenValue.formDefId,
                businessId: data.formValues.hiddenValue.businessId
            }, function (json) {
                if (json.results.url) {
                    empty.hide();
                    $(iframe[0]).load(function () {
                        $(this).height($(document).height());
                    });
                    iframe.attr('src', json.results.url);
                } else {
                    empty.show();
                }
            });
        }
    });
    $$(page.container).find('.tab3').on('show', function () {
        $$(page.navbarInnerContainer).find('.center').text('附件');
        var tab = $$(this),
            ul = tab.find('.list-block ul'),
            empty = tab.find('.empty-view');

        if (data) {
            $$.getJSONP(SERVER_PATH + 'attachmentlist.do', {
                formDefId: data.formValues.hiddenValue.formDefId,
                businessId: data.formValues.hiddenValue.businessId
            }, function (json) {
                if (json.code == 1) {
                    ul.children().remove();
                    $$.each(json.results, function (index, value) {
                        value.suffix = value.ext || 'file';

                        var html = Template7.templates.fileTemplate(value);
                        ul.append(html);
                    });
                    empty[ul.children().length > 0 ? 'hide' : 'show']();
                }
            });
        }
    });
    var confirmList, index = 0;
    $$(page.container).find('.tab4').on('show', function () {
        $$(page.navbarInnerContainer).find('.center').text('流程');
        var tab = $$(this),
            ul = tab.find('.list-block ul');

        if (data && !confirmList) {
            confirmList = data.formValues.confirmList;

            ul.append('<li class="list-group-title">详细申请流程</li>');
            var lastOne = $$('<li class="item-content item-timedot item-timedot-last">' +
                '<div class="item-media"><span></span></div>' +
                '<div class="item-inner">' +
                '<div class="item-subtitle">查看更多</div>' +
                '</div>' +
                '</li>');

            $$.each(confirmList, function (index, value) {
                value.index = confirmList.length - index;
            });
            function loadPage() {
                lastOne.remove();
                for (var i = 0; i < 4 && index < confirmList.length; i++) {
                    var html = Template7.templates.applyConfirmTemplate(confirmList[index++]);
                    ul.append(html);
                }
                if (index < confirmList.length) {
                    ul.append(lastOne);
                }
            }
            lastOne.click(loadPage).click();
        }
    });

    var list = $$(page.container).find('.tab1 .list-block ul');
    $$.getJSONP(SERVER_PATH + 'getDocRelayInfo.do', params, function (json) {
        if (json.code == 1) {
            data = json.results;

            list.append('<li class="list-group-title">' + data.formValues.formtitle + '</li>');

            $$.each(data.formValues.component, function (index, value) {
                value.type = 'display';
            });
            buildComponents(list, data.formValues.component,null);

            var li = $$('<li><div class="item-content"><div class="item-inner row button-row"></div></div></li>');
            li.find('.item-inner').append('<a href="#" data-cmMenuCode="handleDocRelay" class="button active col-100">阅办</a>');
            list.append(li);

            list.find('textarea.resizable').each(function () {
                myApp.resizableTextarea(this);
            }).change();
        }
    });

    list.on('click', '.button.active', function () {
        var menuCode = $$(this).attr('data-cmMenuCode');
        if (menuCode === 'handleDocRelay') {

            myApp.showIndicator();
            $$.getJSONP(SERVER_PATH + 'handleDocRelay.do', {docRelayId: data.docRelayId}, function (json) {
                myApp.hideIndicator();
                if (json.code == 1) {
                    myApp.toast('成功', '<div>√</div>', {}).show(true);
                    page.view.back();
                } else {
                    myApp.toast(json.msg, '<div>×</div>', {}).show(true);
                }
            });
        }
    });
});